/************************************************************************
            Polygons2WingedEdges.cpp - Copyright Claudio Botta

This library is free software; you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or 
(at your option) any later version.
This library is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public License 
along with this library; if not, write to the 
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 
Boston, MA 02110, USA

This file was generated on Wed May 45 2008 at 10:40:32
The original location of this file is geomagick/Polygons2WingedEdges.cpp
**************************************************************************/

#include "Polygons2WingedEdges.h"
using namespace geomagick;

WingedEdges* Polygons2WingedEdges::process(Polygons* inPolygons)
{
	//Triangles* output = new Triangles();
	vector<Vector3d> VertexCoordinates = *(inPolygons->getVertexCoordinates());
	vector<Vector3d> Normals 	  =  *(inPolygons->getNormals());
	vector<Vector2d> TextureCoordinates = *(inPolygons->getTextureCoordinates());
	vector<Polygon> PolygonList  = *(inPolygons->getPolygonList());

	WingedEdges* out = new WingedEdges();
	foreach(Vector3d VertexCoordinate, VertexCoordinates)
	{
		out->addVertexCoordinates(VertexCoordinate);
	}
	
	//The actual conversion
	int FaceNumber = 0;
	foreach(Polygon currentPolygon, PolygonList)
	{

		vector<int> v = currentPolygon.Vertices;
		vector<int> t = currentPolygon.TextureCoordinates;
		vector<int> n = currentPolygon.Normals;
		vector<int>::iterator vi = v.begin();
		vector<int>::iterator ti = t.begin();
		vector<int>::iterator ni = n.begin();
		
		
	
		
		
		//vector<WEdge> newWingedEdges;
		vi++;
		int edge_nbr = 0;
		while(vi < v.end())
		{
			
			int pred = 0;
			int succ = 0;
			if((vi - v.begin()) > 1) pred = *(vi-2);
			else pred = *(v.end()-1); 
			if((v.end() -vi) > 1) succ = *(vi+1);
			else succ = *(v.begin());
			#if DEBUG
				cout << "pred " << pred << endl;
				cout << "from " << *(vi-1) << endl;
				cout << "to " << *(vi) << endl;
				cout << "after " << succ << endl;
			#endif
			edge_nbr = out->addEdgeFromTo(pred,*(vi-1),*(vi),succ,FaceNumber);
			out->VertexSetIncidentEdge((vi-v.begin()),edge_nbr);
			vi++;
		}
		
		out->addFace(edge_nbr);
		
		FaceNumber++;
		
	}

	
	return out;
}
